home *** CD-ROM | disk | FTP | other *** search
/ The Fatted Calf / The Fatted Calf.iso / Applications / UUCP / UUCon / Source / StringStorage.m < prev    next >
Text File  |  1992-11-10  |  2KB  |  90 lines

  1. /*
  2.   Copyright (c) 1992, Nicholas Christopher (nwc@gun.com)
  3.  
  4.     This library is free software; you can redistribute it and/or
  5.     modify it under the terms of the GNU Library General Public
  6.     License as published by the Free Software Foundation; either
  7.     version 2 of the License, or (at your option) any later version.
  8.  
  9.     This library is distributed in the hope that it will be useful,
  10.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  12.     Library General Public License for more details.
  13.  
  14.     You should have received a copy of the GNU Library General Public
  15.     License along with this library; if not, write to the Free
  16.     Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  17. */
  18.  
  19. #import "StringStorage.h"
  20. #import <string.h>
  21.  
  22. static char EOS = (char)0;
  23.  
  24. @implementation StringStorage
  25.  
  26. - init
  27. {
  28.    [self init: ""];
  29.    return self;
  30. }
  31.  
  32. - init: (const char *) str
  33. {
  34.    [super init];
  35.    
  36.    [self initCount: 0 elementSize: sizeof(char) description: "c"];
  37.    [self setStringValue: str];
  38.    return self;
  39. }
  40.  
  41. - setStringValue: (const char *) str;
  42. {
  43.    int len;
  44.  
  45.    if(!str)
  46.        str = "";                 /* avoid nil strings */
  47.  
  48.    len = strlen(str);                 /* get length - no reason to do it twice */
  49.    [self setNumSlots: len + 1];             /* use setNumSlots vs setAvailCapacity since it sets count */
  50.  
  51.                          /* bcopy is far faster than strcpy if the length is known */
  52.    bcopy(str, (char *)dataPtr, len); 
  53.    [self replaceElementAt: len with: (char *)&EOS];
  54.    return self;
  55. }
  56.  
  57. - (const char *) stringValue
  58. {
  59.    return dataPtr;
  60. }
  61.  
  62. - appendStringValue: (const char *)str
  63. {
  64.    int oldCount = [self count];
  65.    int len;
  66.  
  67.    if(!str || !*str)                 /* nothing to append */
  68.        return self;
  69.    
  70.    len = strlen(str);                 
  71.    [self setNumSlots: oldCount + len];
  72.    bcopy(str, (char *)[self elementAt: oldCount - 1], len);
  73.    [self replaceElementAt: oldCount + len - 1 with: (char *)&EOS ];
  74.    return self;
  75. }
  76.  
  77. - appendCharValue: (char) c
  78. {
  79.    char buf[2];
  80.    
  81.    buf[0] = c;
  82.    buf[1] = (char)0;
  83.  
  84.    [self appendStringValue: buf];
  85.  
  86.    return self;
  87. }
  88.  
  89. @end
  90.